package com.common.library.tools;

import android.text.TextUtils;
import android.util.Log;

/**
 * 日志打印工具
 * @note updated by chaosxing on 2016-09-22
 * @note updated by chaosxing on 2016-11-22 普通log合并logShiTou
 */
public class Logger {

    //自定义用于筛选的tag
    private static String TAG_PREFIX = "com.logger";
    private static int LOG_MAXLENGTH = 2000;

    /**
     * 是否打印log,正式包应将以下值设置为false
     */
    private final static boolean allowD = true;
    private final static boolean allowE = true;
    private final static boolean allowI = true;
    private final static boolean allowV = true;
    private final static boolean allowW = true;

    private static StackTraceElement getCallerStackTraceElement() {
        return Thread.currentThread().getStackTrace()[4];
    }

    private static String generateTag(StackTraceElement caller) {
        String tag = "%s.%s(Line:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName
                .lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(),
                caller.getLineNumber());
        tag = TextUtils.isEmpty(TAG_PREFIX) ? tag : TAG_PREFIX + ":"
                + tag;
        return tag;
    }

    public static void d(String tag, Object msgLog) {
        if (!allowD)
            return;

        String msg = msgLog == null ? "" : msgLog.toString();
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAXLENGTH;
        while (true) {
            if (strLength > end) {
                Log.d(tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAXLENGTH;
            } else {
                Log.d(tag, msg.substring(start, strLength));
                break;
            }
        }
    }
    public static void d(Object msgLog) {
        if (!allowD)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        d(tag, msgLog);
    }

    public static void e(Object msgLog) {
        if (!allowE)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);


        String msg = msgLog == null ? "" : msgLog.toString();
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAXLENGTH;
        while (true) {
            if (strLength > end) {
                Log.e(tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAXLENGTH;
            } else {
                Log.e(tag, msg.substring(start, strLength));
                break;
            }
        }
    }

    public static void i(String tag, Object msgLog) {
        if (!allowI)
            return;

        String msg = msgLog == null ? "" : msgLog.toString();
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAXLENGTH;
        while (true) {
            if (strLength > end) {
                Log.i(tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAXLENGTH;
            } else {
                Log.i(tag, msg.substring(start, strLength));
                break;
            }
        }
    }
    public static void i(Object msgLog) {
        if (!allowI)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        i(tag, msgLog);
    }

    public static void v(Object msgLog) {
        if (!allowV)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);


        String msg = msgLog == null ? "" : msgLog.toString();
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAXLENGTH;
        while (true) {
            if (strLength > end) {
                Log.v(tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAXLENGTH;
            } else {
                Log.v(tag, msg.substring(start, strLength));
                break;
            }
        }
    }

    public static void w(String tag, Object msgLog) {
        String msg = msgLog == null ? "" : msgLog.toString();
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAXLENGTH;
        while (true) {
            if (strLength > end) {
                Log.v(tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAXLENGTH;
            } else {
                Log.v(tag, msg.substring(start, strLength));
                break;
            }
        }
    }

    public static void w(Object msgLog) {
        if (!allowW)
            return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        w(tag, msgLog);
    }

}
